﻿using EnglishAuction.BUS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EnglishAuction
{
    public partial class Default : System.Web.UI.Page
    {
        public const int NUMBER_PRODUCT_ONPAGE = 6;
        protected string JsonDate { get; set; }
        private int pageCurrent = 1;

        private void ProductAuction()
        {
            pagerSanPhamDauGia.PageSize = NUMBER_PRODUCT_ONPAGE;
            //pagerSanPhamDauGia.DataSource = SanPhamBUS.GetProductAuction();
            var datasource = SanPhamBUS.GetAll();
            //var datasource = SanPhamBUS.GetProductAuction().Take(8).ToList();
            //var productDisplayPage = SanPhamBUS.GetAll().Skip(NUMBER_PRODUCT_ONPAGE * (pageCurrent - 1)).Take(NUMBER_PRODUCT_ONPAGE).ToList();
            JsonDate = Utilities.GetJsonDate(datasource, pageCurrent);
            pagerSanPhamDauGia.DataSource = datasource;
            pagerSanPhamDauGia.BindToControl = dtlSanPhamDauGia;

            dtlSanPhamDauGia.DataSource = pagerSanPhamDauGia.DataSourcePaged;
            dtlSanPhamDauGia.DataBind();
        }

        private void ProductPass()
        {
            //pagerSanPhamDaDau.PageSize = 8;
            //pagerSanPhamDaDau.DataSource = SanPhamBUS.GetProductPass();
            //pagerSanPhamDaDau.BindToControl = dtlSanPhamDaDau;
            var sanPhams = SanPhamBUS.GetAllDaDauGia();
            var tmp = from n in sanPhams
                      select new
                      {
                          Id = n.Id,
                          HinhAnh = n.HinhAnh,
                          TenSanPham = n.TenSanPham,
                          NgayLenSan = n.NgayLenSan,
                          ThoiGianDauGia = n.ThoiGianDauGia,
                          GiaSan = n.GiaSan,
                          GiaThang = NguoiThangCuocBUS.CheckExist(n.Id) == null ? "Không ai tham gia đấu giá" : String.Format("Giá thắng: <b>{0:###,###} (VNĐ)</b>", NguoiThangCuocBUS.CheckExist(n.Id).Gia),
                      };
            dtlSanPhamDaDau.DataSource = tmp;
            dtlSanPhamDaDau.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["Page"] != null)
            {
                pageCurrent = int.Parse(Request.QueryString["Page"].ToString());
            }

            string key = Request.QueryString["name"]; ;
            if (key != null && !key.ToString().Equals(""))
            {
                pagerSanPhamDauGia.PageSize = NUMBER_PRODUCT_ONPAGE;
                //pagerSanPhamDauGia.DataSource = SanPhamBUS.GetProductAuction();
                var datasource = SanPhamBUS.Search(key.ToString());
                //var datasource = SanPhamBUS.GetProductAuction().Take(8).ToList();
                //var productDisplayPage = SanPhamBUS.GetAll().Skip(NUMBER_PRODUCT_ONPAGE * (pageCurrent - 1)).Take(NUMBER_PRODUCT_ONPAGE).ToList();
                JsonDate = Utilities.GetJsonDate(datasource, pageCurrent);
                pagerSanPhamDauGia.DataSource = datasource;
                pagerSanPhamDauGia.BindToControl = dtlSanPhamDauGia;

                dtlSanPhamDauGia.DataSource = pagerSanPhamDauGia.DataSourcePaged;
                dtlSanPhamDauGia.DataBind();
            }
            else
            {
                ProductAuction();
                ProductPass();
            }
        }
    }
}